<?php
/**
 * @author 天涯 <mail@phpha.com>
 * @copyright http://blog.phpha.com
 * @link http://blog.phpha.com/vncms
 * @version 1.0
 */
defined('APP_PATH') or exit('Access Denied');
require 'check.php';
require 'comm/Include/UploadFile.class.php';
class upld extends check{
	//选择图片
	public function img(){
		if(!empty($_GET['formnum'])){
			$this->formnum = intval($_GET['formnum']);
		}
		if(!empty($_GET['needthumb'])){
			$this->needthumb = trim($_GET['needthumb']);
		}
		if(!empty($_GET['callthumb'])){
			$this->callthumb = trim($_GET['callthumb']);
		}
		$this->thumbConf = spClass('m_conf')->find(null, null, 'thumbw,thumbh');
		$this->display('upld_img.tpl.php');
	}
	//处理上传
	public function doup(){
		if(!empty($_POST['formnum'])){
			$this->formnum = intval($_POST['formnum']);
		}
		if(!empty($_POST['callthumb'])){
			$this->callthumb = trim($_POST['callthumb']);
		}
		//上传配置
		$upload = new UploadFile();
		$upload->maxSize = 1024*1024*2; //2M
		$upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');
		$upload->savePath =  'upload/image/' . date('Ymd') . '/';
		//生成缩略图
		if(!empty($_POST['makethumb'])){
			$thumbConf = spClass('m_conf')->find(null, null, 'thumbw,thumbh');
			$upload->thumb = true;
			$upload->thumbMaxWidth = max($thumbConf['thumbw'], intval($_POST['width']));
			$upload->thumbMaxHeight = max($thumbConf['thumbh'], intval($_POST['height']));
			$this->needthumb = 'yes';
		}else{
			$this->needthumb = 'no';
		}
		//上传结果
		if(!$upload->upload()){
			$this->error = $upload->getErrorMsg();
			$this->display('upld_err.tpl.php');
		}else{
			$extenrow = array('jpg'=>0, 'gif'=>1, 'png'=>2, 'jpeg'=>3);
			$success = $upload->getUploadFileInfo();
			$extension = strtolower($success[0]['extension']);
			$type = $extenrow[$extension];
			$photo = $success[0]['savepath'] . $success[0]['savename'];
			$thumb = $success[0]['savepath'] . 'thumb_' . $success[0]['savename'];
			if($this->needthumb == 'yes'){
				spClass('m_thumb')->create(array('type'=>$type, 'photo'=>$photo, 'thumb'=>$thumb, 'time'=>time()));
			}else{
				spClass('m_thumb')->create(array('type'=>$type, 'photo'=>$photo, 'thumb'=>'', 'time'=>time()));
			}
			$this->saveurl = array('thumb'=>$thumb, 'photo'=>$photo);
			$this->display('upld_suc.tpl.php');
		}
	}
}